
import LanguageMng from "../../Managers/LanguageMng";
import LogicConfigTs from "../../Utils/LogicConfigTs";
import RoomController from "../../Managers/RoomController";
import GameLogicUtilTs from "../../Utils/GameLogicUtilTs";

cc.Class({
    extends: cc.Component,

    properties: {
        bgNode: {
            default: null,
            type: cc.Node
        },

        labelName: {
            default: null,
            type: cc.Label,
        },

        labelPointValue: {
            default: null,
            type: cc.Label,
        },

        labelEntryFee: {
            default: null,
            type: cc.Label,
        },

        labelPlayerCount: {
            default: null,
            type: cc.Label,
        },

        playButtonSprite: {
            default: null,
            type: cc.Sprite
        },

        playFrameGreen: {
            default: null,
            type: cc.SpriteFrame
        },

        playFrameRed: {
            default: null,
            type: cc.SpriteFrame
        },

        playButtonLabel: {
            default: null,
            type: cc.Label
        },

        pointValueIconSprite: {
            default: null,
            type: cc.Sprite
        },

        entryFeeIconSprite: {
            default: null,
            type: cc.Sprite
        },

        playNumLabel: {
            default: null,
            type: cc.Label
        },

        playNumSprite: {
            default: null,
            type: cc.Sprite
        },

        TwoPlayerSprite: {
            default: null,
            type: cc.SpriteFrame
        },

        SixPlayerSprite: {
            default: null,
            type: cc.SpriteFrame
        },

        twoPlayer: {
            default: null,
            type: cc.Node
        },

        sixPlayer: {
            default: null,
            type: cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    tryGetHeight() {
        return this.node.height
    },

    updateItem(info) {
        this.itemInfo = info
        let color;
        let obj = info.data;
        let index = info.index;
        if(index > 25) {
            let aaa = info.data
        }
        if (index % 2 === 0) {
            color = (new cc.color()).fromHEX("#4762A4")
        } else {
            color = (new cc.color()).fromHEX("#354e8d")
        }
        if (info.id == 1) {
            this.initLobbyRoomItem(color, obj.name, obj.multi, obj.e_fee, obj.p_max , obj.id)
        } else if (info.id == 2) {
            this.initLobbyRoomItem(color, obj.name, obj.e_fee, obj.e_fee * obj.p_max, obj.p_max , obj.id)
        }
        this.roomObj = obj

        let key = ""
        if (this.roomObj.type === cc.gg.enumConfig.RoomType.POINTS) {
            key = "point"
        } else if (this.roomObj.type === cc.gg.enumConfig.RoomType.DEAL) {
            key = "deal"
        } else if (this.roomObj.type === cc.gg.enumConfig.RoomType.POOL) {
            key = "pool"
        }

        if(cc.gg.dataMgr.statistics) {
            for (let j = 0; j < cc.gg.dataMgr.statistics[key].length; j++) {
                let countObj = cc.gg.dataMgr.statistics[key][j]
                if (countObj.id === this.roomObj.id) {
                    this.labelPlayerCount.string = countObj.num
                    break;
                }
            }
        }
    },

    // update (dt) {},

    initLobbyRoomItem: function (color, roomName, pointValue, entryFee, playerCount, roomId) {
        if (!color && !roomName && !pointValue && !entryFee && !playerCount) {
            this.bgNode.opacity = 0
            this.bgNode.active = false;
            return;
        } else {
            this.bgNode.opacity = 255
            this.bgNode.active = true;
        }
        this.bgNode.color = color
        this.labelName.string = roomName
        this.labelPointValue.string = cc.gg.gameDataUtil.showRupee ? cc.gg.gameDataUtil.getStringOfRupee(pointValue) : this._getFeeStringWithK(pointValue)
        this.labelEntryFee.string = cc.gg.gameDataUtil.showRupee ? cc.gg.gameDataUtil.getStringOfRupee(entryFee) : this._getFeeStringWithK(entryFee)
        this.labelPlayerCount.string = playerCount
        // this.playNumLabel.string = playerCount;
        if (playerCount == 2) {
            this.twoPlayer.opacity = 255;
            this.sixPlayer.opacity = 0;
        } else if (playerCount == 6) {
            this.sixPlayer.opacity = 255;
            this.twoPlayer.opacity = 0;
        }

        if (!entryFee || entryFee === 0) {
            this.playButtonLabel.string = LanguageMng.getSentence(28)   //Practise
            this.playButtonSprite.spriteFrame = this.playFrameRed
        } else {
            if(LogicConfigTs.LAST_ENTERED_ROOM == roomId){
                this.playButtonLabel.string = "ReJoin"   //ReJoin
                this.playButtonSprite.spriteFrame = this.playFrameRed
            }else{
                this.playButtonLabel.string = LanguageMng.getSentence(36)   //Play Now
                this.playButtonSprite.spriteFrame = this.playFrameGreen
            }
        }

        if (cc.gg.gameDataUtil.showRupee) {
            this.pointValueIconSprite.spriteFrame = cc.gg.gameDataUtil.rupeeIcon
            this.entryFeeIconSprite.spriteFrame = cc.gg.gameDataUtil.rupeeIcon
        } else {
            this.pointValueIconSprite.spriteFrame = cc.gg.gameDataUtil.coinIcon
            this.entryFeeIconSprite.spriteFrame = cc.gg.gameDataUtil.coinIcon
        }
    },

    updatePlayerCount: function (playerCount) {
        this.labelPlayerCount.string = playerCount
    },

    onLobbyItemPlayClicked: function (event) {
        // cc.find("Canvas").emit("server_find_room", this.roomObj)
        GameLogicUtilTs.wantEnterRoomType = this.roomObj.type
        RoomController.startFindRoom(this.roomObj)

        let kv = {
            keys: ["房间类型", "房间名称", "房间人数"],
            values: [cc.gg.enumConfig.getNameByRoomType(this.roomObj.type), this.roomObj.name, this.roomObj.p_max.toString()]
        }
        let jsonStr = JSON.stringify(kv)
        cc.log("JS:TD发送的JSON STRING:", jsonStr)
        cc.gg.tdUtil.doSendTdEvent("click_play", null, jsonStr)
    },

    _getFeeStringWithK: function (number) {
        let mo = number % 1000
        if (number === 0 || mo !== 0) {
            return number
        }

        let newNum = number / 1000
        return newNum + "K"
    },

    _getFeeK: function (number) {
        let mo = number % 1000
        if (number === 0 || mo !== 0) {
            return number
        }

        let newNum = number / 1000
        return newNum
    },
});
